<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Implementing copyable and movable classes</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../move.html" title="Chapter 25. Boost.Move">
<link rel="prev" href="introduction.html" title="Introduction">
<link rel="next" href="composition_inheritance.html" title="Composition or inheritance">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="introduction.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../move.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="composition_inheritance.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="move.implementing_movable_classes"></a><a class="link" href="implementing_movable_classes.html" title="Implementing copyable and movable classes">Implementing copyable
    and movable classes</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="implementing_movable_classes.html#move.implementing_movable_classes.copyable_and_movable_cpp0x">Copyable
      and movable classes in C++0x</a></span></dt>
<dt><span class="section"><a href="implementing_movable_classes.html#move.implementing_movable_classes.copyable_and_movable_cpp03">Copyable
      and movable classes in portable syntax for both C++03 and C++0x compilers</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="move.implementing_movable_classes.copyable_and_movable_cpp0x"></a><a class="link" href="implementing_movable_classes.html#move.implementing_movable_classes.copyable_and_movable_cpp0x" title="Copyable and movable classes in C++0x">Copyable
      and movable classes in C++0x</a>
</h3></div></div></div>
<p>
        Consider a simple handle class that owns a resource and also provides copy
        semantics (copy constructor and assignment). For example a <code class="computeroutput"><span class="identifier">clone_ptr</span></code> might own a pointer, and call
        <code class="computeroutput"><span class="identifier">clone</span><span class="special">()</span></code>
        on it for copying purposes:
      </p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">clone_ptr</span>
<span class="special">{</span>
   <span class="keyword">private</span><span class="special">:</span>
   <span class="identifier">T</span><span class="special">*</span> <span class="identifier">ptr</span><span class="special">;</span>

   <span class="keyword">public</span><span class="special">:</span>
   <span class="comment">// construction</span>
   <span class="keyword">explicit</span> <span class="identifier">clone_ptr</span><span class="special">(</span><span class="identifier">T</span><span class="special">*</span> <span class="identifier">p</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">ptr</span><span class="special">(</span><span class="identifier">p</span><span class="special">)</span> <span class="special">{}</span>

   <span class="comment">// destruction</span>
   <span class="special">~</span><span class="identifier">clone_ptr</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">delete</span> <span class="identifier">ptr</span><span class="special">;</span> <span class="special">}</span>

   <span class="comment">// copy semantics</span>
   <span class="identifier">clone_ptr</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">clone_ptr</span><span class="special">&amp;</span> <span class="identifier">p</span><span class="special">)</span>
      <span class="special">:</span> <span class="identifier">ptr</span><span class="special">(</span><span class="identifier">p</span><span class="special">.</span><span class="identifier">ptr</span> <span class="special">?</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">-&gt;</span><span class="identifier">clone</span><span class="special">()</span> <span class="special">:</span> <span class="number">0</span><span class="special">)</span> <span class="special">{}</span>

   <span class="identifier">clone_ptr</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span><span class="keyword">const</span> <span class="identifier">clone_ptr</span><span class="special">&amp;</span> <span class="identifier">p</span><span class="special">)</span>
   <span class="special">{</span>
      <span class="keyword">if</span> <span class="special">(</span><span class="keyword">this</span> <span class="special">!=</span> <span class="special">&amp;</span><span class="identifier">p</span><span class="special">)</span>
      <span class="special">{</span>
         <span class="identifier">T</span> <span class="special">*</span><span class="identifier">p</span> <span class="special">=</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">ptr</span> <span class="special">?</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">-&gt;</span><span class="identifier">clone</span><span class="special">()</span> <span class="special">:</span> <span class="number">0</span><span class="special">;</span>
         <span class="keyword">delete</span> <span class="identifier">ptr</span><span class="special">;</span>
         <span class="identifier">ptr</span> <span class="special">=</span> <span class="identifier">p</span><span class="special">;</span>
      <span class="special">}</span>
      <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span>
   <span class="special">}</span>

   <span class="comment">// move semantics</span>
   <span class="identifier">clone_ptr</span><span class="special">(</span><span class="identifier">clone_ptr</span><span class="special">&amp;&amp;</span> <span class="identifier">p</span><span class="special">)</span>
      <span class="special">:</span> <span class="identifier">ptr</span><span class="special">(</span><span class="identifier">p</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">ptr</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="special">}</span>

   <span class="identifier">clone_ptr</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span><span class="identifier">clone_ptr</span><span class="special">&amp;&amp;</span> <span class="identifier">p</span><span class="special">)</span>
   <span class="special">{</span>
      <span class="keyword">if</span><span class="special">(</span><span class="keyword">this</span> <span class="special">!=</span> <span class="special">&amp;</span><span class="identifier">p</span><span class="special">)</span>
      <span class="special">{</span>
         <span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span><span class="special">(</span><span class="identifier">ptr</span><span class="special">,</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">);</span>
         <span class="keyword">delete</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">;</span>
         <span class="identifier">p</span><span class="special">.</span><span class="identifier">ptr</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
      <span class="special">}</span>
      <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span>
   <span class="special">}</span>

   <span class="comment">// Other operations...</span>
<span class="special">};</span>
</pre>
<p>
        <code class="computeroutput"><span class="identifier">clone_ptr</span></code> has expected copy
        constructor and assignment semantics, duplicating resources when copying.
        Note that copy constructing or assigning a <code class="computeroutput"><span class="identifier">clone_ptr</span></code>
        is a relatively expensive operation:
      </p>
<p>
</p>
<pre class="programlisting"><span class="identifier">clone_ptr</span><span class="special">&lt;</span><span class="identifier">Base</span><span class="special">&gt;</span> <span class="identifier">p1</span><span class="special">(</span><span class="keyword">new</span> <span class="identifier">Derived</span><span class="special">());</span>
<span class="comment">// ...</span>
<span class="identifier">clone_ptr</span><span class="special">&lt;</span><span class="identifier">Base</span><span class="special">&gt;</span> <span class="identifier">p2</span> <span class="special">=</span> <span class="identifier">p1</span><span class="special">;</span>  <span class="comment">// p2 and p1 each own their own pointer</span>
</pre>
<p>
      </p>
<p>
        <code class="computeroutput"><span class="identifier">clone_ptr</span></code> is code that you
        might find in today's books on C++, except for the part marked as <code class="computeroutput"><span class="identifier">move</span> <span class="identifier">semantics</span></code>.
        That part is implemented in terms of C++0x <code class="computeroutput"><span class="identifier">rvalue</span>
        <span class="identifier">references</span></code>. You can find some good
        introduction and tutorials on rvalue references in these papers:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            <a href="http://www.artima.com/cppsource/rvalue.html" target="_top"><span class="emphasis"><em>A
            Brief Introduction to Rvalue References</em></span></a>
          </li>
<li class="listitem">
            <a href="http://blogs.msdn.com/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx" target="_top"><span class="emphasis"><em>Rvalue
            References: C++0x Features in VC10, Part 2</em></span></a>
          </li>
</ul></div>
<p>
        When the source of the copy is known to be a <code class="computeroutput"><span class="identifier">rvalue</span></code>
        (e.g.: a temporary object), one can avoid the potentially expensive <code class="computeroutput"><span class="identifier">clone</span><span class="special">()</span></code>
        operation by pilfering source's pointer (no one will notice!). The move constructor
        above does exactly that, leaving the rvalue in a default constructed state.
        The move assignment operator simply does the same freeing old resources.
      </p>
<p>
        Now when code tries to copy a rvalue <code class="computeroutput"><span class="identifier">clone_ptr</span></code>,
        or if that code explicitly gives permission to consider the source of the
        copy a rvalue (using <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span></code>),
        the operation will execute much faster.
      </p>
<p>
</p>
<pre class="programlisting"><span class="identifier">clone_ptr</span><span class="special">&lt;</span><span class="identifier">Base</span><span class="special">&gt;</span> <span class="identifier">p1</span><span class="special">(</span><span class="keyword">new</span> <span class="identifier">Derived</span><span class="special">());</span>
<span class="comment">// ...</span>
<span class="identifier">clone_ptr</span><span class="special">&lt;</span><span class="identifier">Base</span><span class="special">&gt;</span> <span class="identifier">p2</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">p1</span><span class="special">);</span>  <span class="comment">// p2 now owns the pointer instead of p1</span>
<span class="identifier">p2</span> <span class="special">=</span> <span class="identifier">clone_ptr</span><span class="special">&lt;</span><span class="identifier">Base</span><span class="special">&gt;(</span><span class="keyword">new</span> <span class="identifier">Derived</span><span class="special">());</span>   <span class="comment">// temporary is moved to p2</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="move.implementing_movable_classes.copyable_and_movable_cpp03"></a><a class="link" href="implementing_movable_classes.html#move.implementing_movable_classes.copyable_and_movable_cpp03" title="Copyable and movable classes in portable syntax for both C++03 and C++0x compilers">Copyable
      and movable classes in portable syntax for both C++03 and C++0x compilers</a>
</h3></div></div></div>
<p>
        Many aspects of move semantics can be emulated for compilers not supporting
        <code class="computeroutput"><span class="identifier">rvalue</span> <span class="identifier">references</span></code>
        and <span class="bold"><strong>Boost.Move</strong></span> offers tools for that purpose.
        With <span class="bold"><strong>Boost.Move</strong></span> we can write <code class="computeroutput"><span class="identifier">clone_ptr</span></code> so that it will work both in
        compilers with rvalue references and those who conform to C++03. You just
        need to follow these simple steps:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            Put the following macro in the <span class="bold"><strong>private</strong></span>
            section: <code class="computeroutput"><a class="link" href="../BOOST_COPYABLE_AND_MOVABLE.html" title="Macro BOOST_COPYABLE_AND_MOVABLE">BOOST_COPYABLE_AND_MOVABLE(classname)</a></code>
          </li>
<li class="listitem">
            Leave copy constructor as is.
          </li>
<li class="listitem">
            Write a copy assignment taking the parameter as <code class="computeroutput"><a class="link" href="../BOOST_COPY_ASSIGN_REF.html" title="Macro BOOST_COPY_ASSIGN_REF">BOOST_COPY_ASSIGN_REF(classname)</a></code>
          </li>
<li class="listitem">
            Write a move constructor and a move assignment taking the parameter as
            <code class="computeroutput"><a class="link" href="../BOOST_RV_REF.html" title="Macro BOOST_RV_REF">BOOST_RV_REF(classname)</a></code>
          </li>
</ul></div>
<p>
        Let's see how are applied to <code class="computeroutput"><span class="identifier">clone_ptr</span></code>:
      </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">clone_ptr</span>
<span class="special">{</span>
   <span class="keyword">private</span><span class="special">:</span>
   <span class="comment">// Mark this class copyable and movable</span>
   <span class="identifier">BOOST_COPYABLE_AND_MOVABLE</span><span class="special">(</span><span class="identifier">clone_ptr</span><span class="special">)</span>
   <span class="identifier">T</span><span class="special">*</span> <span class="identifier">ptr</span><span class="special">;</span>

   <span class="keyword">public</span><span class="special">:</span>
   <span class="comment">// Construction</span>
   <span class="keyword">explicit</span> <span class="identifier">clone_ptr</span><span class="special">(</span><span class="identifier">T</span><span class="special">*</span> <span class="identifier">p</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">ptr</span><span class="special">(</span><span class="identifier">p</span><span class="special">)</span> <span class="special">{}</span>

   <span class="comment">// Destruction</span>
   <span class="special">~</span><span class="identifier">clone_ptr</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">delete</span> <span class="identifier">ptr</span><span class="special">;</span> <span class="special">}</span>

   <span class="identifier">clone_ptr</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">clone_ptr</span><span class="special">&amp;</span> <span class="identifier">p</span><span class="special">)</span> <span class="comment">// Copy constructor (as usual)</span>
      <span class="special">:</span> <span class="identifier">ptr</span><span class="special">(</span><span class="identifier">p</span><span class="special">.</span><span class="identifier">ptr</span> <span class="special">?</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">-&gt;</span><span class="identifier">clone</span><span class="special">()</span> <span class="special">:</span> <span class="number">0</span><span class="special">)</span> <span class="special">{}</span>

   <span class="identifier">clone_ptr</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span><span class="identifier">BOOST_COPY_ASSIGN_REF</span><span class="special">(</span><span class="identifier">clone_ptr</span><span class="special">)</span> <span class="identifier">p</span><span class="special">)</span> <span class="comment">// Copy assignment</span>
   <span class="special">{</span>
      <span class="keyword">if</span> <span class="special">(</span><span class="keyword">this</span> <span class="special">!=</span> <span class="special">&amp;</span><span class="identifier">p</span><span class="special">){</span>
         <span class="identifier">T</span> <span class="special">*</span><span class="identifier">tmp_p</span> <span class="special">=</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">ptr</span> <span class="special">?</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">-&gt;</span><span class="identifier">clone</span><span class="special">()</span> <span class="special">:</span> <span class="number">0</span><span class="special">;</span>
         <span class="keyword">delete</span> <span class="identifier">ptr</span><span class="special">;</span>
         <span class="identifier">ptr</span> <span class="special">=</span> <span class="identifier">tmp_p</span><span class="special">;</span>
      <span class="special">}</span>
      <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span>
   <span class="special">}</span>

   <span class="comment">//Move semantics...</span>
   <span class="identifier">clone_ptr</span><span class="special">(</span><span class="identifier">BOOST_RV_REF</span><span class="special">(</span><span class="identifier">clone_ptr</span><span class="special">)</span> <span class="identifier">p</span><span class="special">)</span>            <span class="comment">//Move constructor</span>
      <span class="special">:</span> <span class="identifier">ptr</span><span class="special">(</span><span class="identifier">p</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">ptr</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="special">}</span>

   <span class="identifier">clone_ptr</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span><span class="identifier">BOOST_RV_REF</span><span class="special">(</span><span class="identifier">clone_ptr</span><span class="special">)</span> <span class="identifier">p</span><span class="special">)</span> <span class="comment">//Move assignment</span>
   <span class="special">{</span>
      <span class="keyword">if</span> <span class="special">(</span><span class="keyword">this</span> <span class="special">!=</span> <span class="special">&amp;</span><span class="identifier">p</span><span class="special">){</span>
         <span class="keyword">delete</span> <span class="identifier">ptr</span><span class="special">;</span>
         <span class="identifier">ptr</span> <span class="special">=</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">;</span>
         <span class="identifier">p</span><span class="special">.</span><span class="identifier">ptr</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
      <span class="special">}</span>
      <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span>
   <span class="special">}</span>
<span class="special">};</span>
</pre>
<p>
      </p>
</div>
<p>
      <span class="bold"><strong>Question</strong></span>: What about types that don't own
      resources? (E.g. <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span></code>?)
    </p>
<p>
      No work needs to be done in that case. The copy constructor is already optimal.
    </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2008-2014 Ion Gaztanaga<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="introduction.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../move.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="composition_inheritance.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
